package raft.entity;

import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
@Setter
@Getter
public class LogEntry implements Serializable,Comparable {


    /**
     * 索引
     */
    private Long index;

    /**
     * 任期
     */
    private long term;

    /**
     * 操作
     */
    private Command command;

    public LogEntry(Builder builder){
        setCommand(builder.command);
        setIndex(builder.index);
        setTerm(builder.term);
    }

    public LogEntry(long term, Command command) {
        this.term = term;
        this.command = command;
    }

    public LogEntry(Long index, long term, Command command) {
        this.index = index;
        this.term = term;
        this.command = command;
    }


    public static Builder newBuilder(){
        return new Builder();
    }

    @Override
    public int compareTo(Object o) {
        if (o == null) {
            return -1;
        }
        if (this.getIndex() > ((LogEntry) o).getIndex()) {
            return 1;
        }
        return -1;
    }

    public static final class Builder{
        private Long index;

        private long term;

        private Command command;

        public Builder index(long val){
            this.index = val;
            return this;
        }

        public Builder term(long val){
            this.term = val;
            return this;
        }

        public Builder Command(Command val){
            this.command = val;
            return this;
        }

        public LogEntry builder(){
            return new LogEntry(this);
        }

    }



}
